//
//  SISSHouseCoopTakeLookRecordViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/9.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSHouseCoopTakeLookRecordViewController.h"
#import "SISSHouseTakeLookRecordCell.h"
#import "SISSHouseCoopTakeLookDetailViewController.h"
#import "SISSAddHouseCoopTakeLookViewController.h"
#import "SISSNetworkingManager+CoopProgress.h"

@interface SISSHouseCoopTakeLookRecordViewController ()

/* 带看模型数组 */
@property (strong, nonatomic) NSArray<SISSHouseCoopTakeLookModel *> *takeLookArr;

@end

@implementation SISSHouseCoopTakeLookRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self requestHouseTakeLookRecordData];
}

- (void)addRecordButtonDidClickAction {
    [super addRecordButtonDidClickAction];
    
    SISSAddHouseCoopTakeLookViewController *vc = [[SISSAddHouseCoopTakeLookViewController alloc] init];
    vc.houseCoopProgressModel = self.houseCoopProgressModel;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.takeLookArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSHouseTakeLookRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:houseTakeLookRecordCellReuseID forIndexPath:indexPath];
    SISSHouseCoopTakeLookModel *model = self.takeLookArr[indexPath.row];
    model.type = self.houseCoopProgressModel.type;
    cell.takeLookModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    SISSHouseCoopTakeLookDetailViewController *vc = [[SISSHouseCoopTakeLookDetailViewController alloc] init];
    vc.houseCoopProgressModel = self.houseCoopProgressModel;
    vc.deleteCompletedBlock = ^{
        
        [self requestHouseTakeLookRecordData];
    };
    vc.brokerState = self.takeLookArr[indexPath.row].brokerState;
    vc.takeLookModel = self.takeLookArr[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)upDownLoadData {
    
    [self requestHouseTakeLookRecordData];
}

#pragma mark - 获取房源带看记录数据
- (void)requestHouseTakeLookRecordData {
    
    [kNetworkManager requestHouseCooperationTakeLookWithAcbId:self.houseCoopProgressModel.ID success:^(NSArray<SISSHouseCoopTakeLookModel *> *takeLookArr) {
        self.takeLookArr = takeLookArr;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        
        [self.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
